<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/10/0010
 * Time: 10:27
 */

namespace app\sports_admin\model;


use think\Model;

class LoginLog extends Model
{
    protected $table = 'login_log';

    protected $pk = 'id';

    protected $autoWriteTimestamp = true;

    protected $hidden = ['update_time'];

    public function getClientIpAttr($value)
    {
        return long2ip($value);
    }
    /**
     * 登录日志表添加一条记录
     * @params Array $params 登录信息
    */
    public static function adminLoginLogInsert($params)
    {
        return self::create($params);
    }
    /**
     * 获取管理员登录日志
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $start 检索的开始时间
     * @param $end 检索的结束时间
    */
    public static function adminLoginLogList($page,$rows,$sort,$sortOrder,$searchName,$start,$end)
    {
        $result = self::where(function($query)use ($searchName,$start,$end){
                if($searchName){
                    $query->where('account','like',"%{$searchName}%");
                }
                if($start && $end){
                    $query->whereTime('create_time','between',["{$start}","{$end}"]);
                }
            })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();
        $total = self::where(function($query)use ($searchName,$start,$end){
                if($searchName){
                    $query->where('account','like',"%{$searchName}%");
                }
                if($start && $end){
                    $query->whereTime('create_time','between',["{$start}","{$end}"]);
                }
            })->count();
        return json(['rows'=>$result,'total'=>$total]);
    }
}